package com.wbvideo.resample;


import android.util.Log;

import java.nio.ByteBuffer;

public class AudioReSampleHelp {
    private final String TAG = "AudioReSampleHelp";
    public static final int FRAME_SIZE = 320;//必须是160*2的整数倍
    private int inByteLen = 0;
    private ByteBuffer mInByteBuffer;
    private int inBufferSize = 0;

    private final int frameBufferLen = 1024 * 2 * 2;
    private long mNativeReSampler = 0;

    private AudioReSampleHelp() {
        audioReSampleInit(frameBufferLen);
    }

    private static final AudioReSampleHelp instance = new AudioReSampleHelp();

    public static AudioReSampleHelp getInstance() {
        return instance;
    }

    private void audioReSampleInit(int byteLen) {
        inByteLen = byteLen;
        mInByteBuffer = ByteBuffer.allocate(4 * byteLen);
        mInByteBuffer.clear();
        inBufferSize = 0;
    }


    public void initReSampler(int inChannels, int inSampleRate, int outChannels,
                              int outSampleRate) {
        synchronized (TAG) {
            mNativeReSampler = AudioReSampleUtils.initReSample(inChannels, inSampleRate,
                    outChannels, outSampleRate);
        }
    }

    public void processAudioData(byte[] buffer, IReSampleProcessListener reSampleProcessListener) {
        synchronized (TAG) {
            if (mNativeReSampler == 0 || null == buffer || buffer.length == 0) {
                if (null != reSampleProcessListener) {
                    reSampleProcessListener.onProcess(buffer);
                }
                return;
            }

            Log.d(TAG, "webRtcNsProcess inBufferSize:" + inBufferSize);
            while (inBufferSize >= frameBufferLen) {
                Log.d(TAG, "webRtcNsProcess mInByteBuffer full");
                reSampleProcessInner(reSampleProcessListener);
            }

            mInByteBuffer.put(buffer);
            inBufferSize += buffer.length;

            if (null != reSampleProcessListener) {
                reSampleProcessListener.onProcess(null);
            }
        }
    }

    public void endResample() {
        synchronized (TAG) {
            if (mNativeReSampler == 0)
                return;
            AudioReSampleUtils.endResample(mNativeReSampler);
        }
    }

    private void reSampleProcessInner(IReSampleProcessListener reSampleProcessListener) {
        byte[] buf = new byte[frameBufferLen];

        mInByteBuffer.rewind();
        mInByteBuffer.get(buf, 0, buf.length);

        byte[] inBufferLeft = new byte[inBufferSize - mInByteBuffer.position()];
        mInByteBuffer.get(inBufferLeft);
        mInByteBuffer.rewind();
        mInByteBuffer.clear();
        inBufferSize = 0;

        mInByteBuffer.put(inBufferLeft);
        inBufferSize += inBufferLeft.length;

        byte[] processData = AudioReSampleUtils.reSampleBuffer(mNativeReSampler, buf, buf.length);

        if (null != reSampleProcessListener) {
            reSampleProcessListener.onProcess(processData);
        }
    }
}
